<template>
  <div class="home-container fadeIn">
    <div class='home-content'>
      <div class='account'>
        <span>运营数据查询</span>
      </div>
      <div class="condition">
        <el-form ref="form" :inline="true" :model="form" class="demo-form-inline">
          <el-form-item label="appkey">
              <el-input v-model='form.appkey' placeholder='请输入appkey' type="text"  auto-complete='off' />
          </el-form-item>

          <el-form-item label="appid">
            <el-input v-model='form.appid' placeholder='请输入appid' type="text"  auto-complete='off' />
          </el-form-item>

          <el-form-item label="时间间距">
            <el-date-picker
              v-model="form.time"
              type="datetimerange"
              format='yyyy-mm-dd'
              :picker-options="pickerOptions2"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              align="right">
            </el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="tableList">
        <el-table
          :data="tableData"
          style="width: 100%"
          :default-sort = "{prop: 'date', order: 'descending'}"
          >
          <el-table-column
            prop="date"
            label="日期"
            sortable
            width="180">
          </el-table-column>
          <el-table-column
            prop="imid"
            label="新增"
            >
          </el-table-column>
          <el-table-column
            prop="click"
            label="点击量"
            sortable
            width="180">
          </el-table-column>
          <el-table-column
            prop="cpc"
            label="CPC"
            >
          </el-table-column>
          <el-table-column
            prop="earn"
            label="收入"
            >
          </el-table-column>
        </el-table>
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="1"
          :page-size="10"
          @current-change='pageChange'
          :total="this.total">
        </el-pagination>

      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import { queryString } from '@/utils';

export default {
  name: 'HomeIndex',
  data() {
    return {
      form: {
        appkey: '',
        appid: '',
        time: '',
      },
      total: 0,
      tableData: [],
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - (3600 * 1000 * 24 * 7));
            picker.$emit('pick', [start, end]);
          },
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - (3600 * 1000 * 24 * 30));
            picker.$emit('pick', [start, end]);
          },
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - (3600 * 1000 * 24 * 90));
            picker.$emit('pick', [start, end]);
          },
        }],
      },
    };
  },
  computed: {
    ...mapState('homeClear', [
      'clearList',
    ]),
  },
  mounted() {
    this.setClearList().then(() => {
      this.total = this.clearList.records;
      this.tableData = this.clearList.dataes;
    });
  },
  components: {
  },
  methods: {
    ...mapActions('homeClear', [
      'setClearList',
    ]),
    sousuo(num) {
      const data = {
        pageNo: num === 0 ? 1 : num,
        pageSize: 10,
        userId: this.$getLS('userId'),
        appkey: this.form.appkey,
        appid: this.form.appid,
        startDate: this.form.time[0] ? this.timeString(this.form.time[0]) : '',
        endDate: this.form.time[1] ? this.timeString(this.form.time[1]) : '',
      };
      this.setClearList(queryString(data)).then(() => {
        this.total = this.clearList.records;
        this.tableData = this.clearList.dataes;
        this.pageNo = num === 0 ? 1 : num;
      });
    },
    onSubmit() {
      this.sousuo(0);
    },
    timeString(date) {
      const time = new Date(date);
      return `${time.getFullYear()}-${time.getMonth() + 1}-${time.getDate()}`;
    },
    pageChange(val) {
      this.sousuo(val);
    },
  },
};
</script>
<style media="screen">
.echarts {
height: 300px;
}
</style>
<style lang='scss' src='@/scss/pages/home_index.scss'/>
